home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / examples / collatz < prev    next >
Text File  |  1995-05-20  |  460b  |  35 lines

  1. //
  2. // The 3n + 1 problem
  3. //
  4.  
  5. collatz = function(start)
  6. {
  7.   local(c, n);
  8.   c = n = start;
  9.  
  10.   while(n > 1)
  11.   {
  12.     if(mod(n,2) == 0) {
  13.       n = n/2;
  14.     else
  15.       n = 3*n + 1;
  16.     }
  17.     c = [c, n];
  18.   }
  19.   return c;
  20. };
  21.  
  22. //
  23. // Try it out
  24. //
  25.  c = collatz(100);
  26.  
  27.  plgrid ();
  28.  pltitle ( "RLaB Collatz Example" );
  29.  xlabel ( "Indeplendent Variable" );
  30.  ylabel ( "Deplendent Variable" );
  31.  plot( [1:c.nc; c]' );
  32.  pause ();
  33.  plgrid ( ,"bcgnstlv");
  34.  plot( [1:c.nc; c]' );
  35.